
#include <sys/types.h>
#include <sys/stat.h>

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "BOOL.h"

/*compares to files */
static BOOL compare_files(char *file1_name, char *file2_name);
/*compares to filesize */
static BOOL compare_filesize(char *file1_name, char *file2_name);

/**/
static void test_huffman()
{
    char *in_file_original = "test_in2.txt";
    char *compressed_name = "test_in2.txt.hc";
    char *decompress_name = "test_in2.txt.hc.hd";
    FILE *p_out = NULL;

    huffman_compress(in_file_original, compressed_name);

    p_out = fopen(compressed_name, "rb");
    assert(p_out != NULL);

    huffman_decompress(compressed_name, decompress_name);

    assert(compare_files(in_file_original, decompress_name) == TRUE);

}

/* ----------------------------------------------------------------------------
 * function: test_filesize
 * ------------------------------------------------------------------------- */
static void test_filesize()
{
    char *in_file_original = "test_in.txt";
    char *compressed_name = "test_in.txt.hc";

    FILE *p_compressed = NULL;

    huffman_compress(in_file_original, compressed_name);

    p_compressed = fopen(compressed_name, "rb");
    assert(p_compressed != NULL);

    assert(compare_filesize(in_file_original, compressed_name) == TRUE);
}

/* ----------------------------------------------------------------------------
 * function: compare_files
 * ------------------------------------------------------------------------- */
static BOOL compare_files(char *file1_name, char *file2_name)
{
    FILE *p_file1 = fopen(file1_name, "rb");
    FILE *p_file2 = fopen(file2_name, "rb");
    BOOL equal = TRUE;
    int file1char = 0;
    int file2char = 0;

    assert(p_file1 != NULL);
    assert(p_file2 != NULL);

    file1char = fgetc(p_file1);
    file2char = fgetc(p_file2);

    while (equal && file1char != EOF && file2char != EOF)
    {
        equal = file1char == file2char;

        file1char = fgetc(p_file1);
        file2char = fgetc(p_file2);
    }

    return equal && file1char == EOF && file2char == EOF;
}

/* ----------------------------------------------------------------------------
 * function: compare_filesize
 * ------------------------------------------------------------------------- */
static BOOL compare_filesize(char *file1_name, char *file2_name)
{
    FILE *p_file1 = fopen(file1_name, "rb");
    FILE *p_file2 = fopen(file2_name, "rb");

    struct stat buf1;
    struct stat buf2;
    long file1size;
    long file2size;

    stat(file1_name, &buf1);
    stat(file2_name, &buf2);

    file1size = buf1.st_size;
    file2size = buf2.st_size;


    assert(p_file1 != NULL);
    assert(p_file2 != NULL);

    printf("Unkomprimiert: %ld\n", file1size);
    printf("Komprimiert: %ld\n", file2size);
    assert(file1size > file2size);


    return file1size>file2size;
}

/* ----------------------------------------------------------------------------
 * function: main
 *
 * start test
 * ------------------------------------------------------------------------- */
int main_test(int argc, char** argv)
{
    printf("test huffman...\n");
     test_huffman();
    printf("test filesize...\n");
    test_filesize();

    return (EXIT_SUCCESS);
}

